package edu.cth.speedapp.teacher;

import java.util.Scanner;

import android.R.integer;
import android.content.Intent;
import android.nfc.Tag;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.cth.speedapp.DeviceUuidFactory;
import edu.cth.speedapp.R;
import edu.cth.speedapp.ScannerActivity;
import edu.cth.speedapp.asynctasks.HTTPAsyncTask;
import edu.cth.speedapp.asynctasks.ITaskCallback;
import edu.cth.speedapp.student.JoinRoomActivity;
import edu.cth.speedapp.student.StudentActivity;

public class TagLinkerActivity extends ScannerActivity implements ITaskCallback{
			
		//Url for http req's. To my super server.
		//public static String baseUrl = "";
		private String roomCode = "";
		private String roomName;
		
		//View handles
		private View rescanButton;
		private View connectionProgress;
		private TextView qrStatusText;
		private Button confirmTagButton;
		private TextView nfcInstructionText;
	    
		
		//Used to suppress rescan on resume when finding an rfid
	    private boolean tagRead = false;
	    
	    
	    	
	     
		    
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_link_tag);

			// Room information
	        Bundle extras = getIntent().getExtras();
	    	
	        if(extras != null ) {
	        	roomName = extras.getString("roomName");
	        	setTitle("Link room: " + roomName);
	        }
	    	
			
			//Disable nfc text if nfc is not supported
			nfcInstructionText = (TextView)findViewById(R.id.nfcInstructionText);
			if(!isNFCPossibleOnDevice()){
				nfcInstructionText.setVisibility(View.GONE);
			}

		
			rescanButton = this.findViewById(R.id.rescanButton);
			rescanButton.setVisibility(View.INVISIBLE);
			
			connectionProgress = this.findViewById(R.id.connectionProgress);
			connectionProgress.setVisibility(View.INVISIBLE);
			
			qrStatusText = (TextView)findViewById(R.id.qrStatusText);
			
			confirmTagButton = (Button)findViewById(R.id.confirmTagButton);
			confirmTagButton.setVisibility(View.INVISIBLE);      
	      
	        
	        
		}
		@Override
		public void tagRead(Tag tag) {
			Log.d("lol", "mytag: lock");
			tagRead = true;
			super.tagRead(tag);
			Log.d("lol", "mytag: unlock");
			
		}

		@Override
		protected void foundCode(String data){
			Log.d("lol", "mytag: foundcode");
			super.foundCode(data);
			roomCode = data;

			qrStatusText.setText("Connect " + roomName + " with code " + data + "?");
			
			confirmTagButton.setVisibility(View.VISIBLE);
			
		}
		public void httpTaskCallback(String results) {
			//Close to nothing
		}
		private void noRoomFound(String errorMessage){
			//Nothing
		}
		
		public void onClickRescan(View v){
			rescan();
		}
		private void rescan(){
			Log.d("lol", "mytag: rescan");
	        qrStatusText.setText(R.string.qr_instruction);
	        rescanButton.setVisibility(View.GONE);
	        confirmTagButton.setVisibility(View.VISIBLE);
	        if(isNFCPossibleOnDevice()){
				nfcInstructionText.setVisibility(View.VISIBLE);
			}
	        startPreview();
	        connectionProgress.setVisibility(View.INVISIBLE);
		}
		
		public void onClickConfirmTag(View v){
			//Intent intent = new Intent(this, JoinRoomActivity.class);
			//startActivity(intent);
			
			qrStatusText.setText("Connecting...");
			if(isNFCPossibleOnDevice()){
				nfcInstructionText.setVisibility(View.INVISIBLE);
			}
			confirmTagButton.setVisibility(View.INVISIBLE);
			connectionProgress.setVisibility(View.VISIBLE);

			
			HTTPAsyncTask connection = new HTTPAsyncTask(this);
			
		     connection.execute(getString(R.string.baseURL) + "/connectRoomWithCode.php?code=" + roomCode + "&&roomID=" + roomName);
			  
			 finish();
			//TODO  Return the found tag.
		}
		
		@Override
	    protected void onResume() {
	    	super.onResume();
	    	if(!tagRead){
	    		rescan();
	    	}else{
	    		tagRead = false;
	    	}
	    }

		@Override
		public FrameLayout getPreviewContainer() {
			return (FrameLayout)findViewById(R.id.qrCameraPreview);
		}
	
}
